package com.smartrobot.smartrobot.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.PauseOnScrollListener;
import com.smartrobot.smartrobot.Config.Config;
import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.adapter.ActiveListViewAdapter;
import com.smartrobot.smartrobot.logToSdCard.LogToSd;
import com.smartrobot.smartrobot.util.Active;
import com.smartrobot.smartrobot.util.FileUtil;
import com.smartrobot.smartrobot.util.HttpUtil;
import com.smartrobot.smartrobot.util.ImageLoaderApi;
import com.smartrobot.smartrobot.util.User;
import com.smartrobot.smartrobot.view.ExListView;
import com.smartrobot.smartrobot.view.ToastCustom;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2014/12/22.
 */
public class ActiveActivity extends Activity implements View.OnClickListener {

    private RelativeLayout toCirlce,toWorld,sendActive;
    private ImageView  back;

    private List<View> views;
    private View circleView;
    private View worldView;

    private ViewPager viewPager;


    //viewPager里面的listview
    public ExListView circleActiveListView;
    public ExListView worldActiveListView;
    //页面开始前的组件 进度条/文字框
    private ProgressBar circlePreProgress;
    private TextView cireclePreText;

    private ProgressBar worldPreProgress;
    private TextView worldPreText;

    //listVeiw的adapter
    private ActiveListViewAdapter circleAdapter;
    private ActiveListViewAdapter worldAdapter;

    //避免多次加载
    private boolean isFirstCircle = true;
    private boolean isFirstWorld = true;

    private Map<String,String> map;
    private List<Active> circleList,worldList;
    //临时的list,用作引用加载数据
    private List<Active> tempNewCircleList,tempNewWorldList;
    private List<Active> tempOLdCircleList,tempOLdWorldList;
    private ActiveHandler handler;

    //当前选择的页面，用作 判断向服务器拿哪一个数据更新
    private int pos;


    //本页面使用，用作 判断是否listview是向上拉还是向下拉
    static public int HANDLER_NEW_UPDATE = 1000;
    static public int HANDLER_OLD_UPDATE = 1001;

    private boolean isGetActicve = false;

    //发布动态返回
    private final int RETURN_FROM_SEND_ACTIVITY = 0;
    //更新提示，只有向上刷新的数据才会有提示“没有了”
    private boolean isUpdateTop = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState != null){
            throw new NullPointerException();
        }
        this.setContentView(R.layout.page_friend_active);


        init();


    }

    void init(){
        initUtil();
        initView();
        initViewPager();
    }

    void initUtil(){
        //初始化httpUtil 服用session
        HttpUtil.init(getApplicationContext());
        LogToSd.log_init();
        Config.config_init(getApplicationContext());
        ImageLoaderApi.initImageLoaderApi(getApplicationContext());
    }

    void initView(){

        back = (ImageView)findViewById(R.id.active_back);
        toCirlce = (RelativeLayout)findViewById(R.id.active_but_my_circle);
        toWorld = (RelativeLayout)findViewById(R.id.active_but_world);
        sendActive = (RelativeLayout)findViewById(R.id.active_send_active);

        //初始化
        handler = new ActiveHandler();
        tempNewCircleList = new ArrayList<Active>();
        tempNewWorldList = new ArrayList<Active>();
        tempOLdCircleList =  new ArrayList<Active>();
        tempOLdWorldList = new ArrayList<Active>();

        //定义 Active场景的ImageLoader
        ImageLoaderApi.initImageLoaderApiActivePic(getApplicationContext());

        back.setOnClickListener(this);
        toCirlce.setOnClickListener(this);
        toWorld.setOnClickListener(this);
        sendActive.setOnClickListener(this);


    }

    void initViewPager(){

        views = new ArrayList<View>();
        circleView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.page_item_listview,null);
        worldView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.page_item_listview,null);

        views.add(circleView);
        views.add(worldView);

        viewPager = (ViewPager)findViewById(R.id.active_viewpager);

        // 为viewpager创建adapter
        PagerAdapter pagerAdapter = new PagerAdapter() {

            @Override
            public boolean isViewFromObject(View view, Object object) {
                // TODO Auto-generated method stub
                return view == object;
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                if(views != null)
                   return views.size();
                else
                    return  0;

            }

            @Override
            public Object instantiateItem(View container, int position) {
                ((ViewPager) container).addView(views.get(position), 0);
                return views.get(position);
            }

            @Override
            public void destroyItem(View container, int position, Object object) {

                ((ViewPager) container).removeView(views.get(position));

            }

        };

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {

            }

            @Override
            public void onPageSelected(int position) {

                if(position == 0){
                    pos = 0;
                    toCirlce.setSelected(true);
                    toWorld.setSelected(false);
                    initCircleActive();
                }else{
                    pos = 1;
                    toCirlce.setSelected(false);
                    toWorld.setSelected(true);
                    initWorldActive();
                }

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });


        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(0);
        pos = 0;
        toCirlce.setSelected(true);
        toWorld.setSelected(false);
        initCircleActive();
        initWorldActive();

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.active_back:
                ActiveActivity.this.finish();
                break;

            case R.id.active_but_my_circle:

                viewPager.setCurrentItem(0);
                initCircleActive();
                pos = 0;
                toCirlce.setSelected(true);
                toWorld.setSelected(false);

                break;

            case R.id.active_but_world:

                viewPager.setCurrentItem(1);
                initWorldActive();
                pos = 1;
                toCirlce.setSelected(false);
                toWorld.setSelected(true);
                break;

            case R.id.active_send_active:
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(),ActiveSendActivity.class);
                intent.putExtra("pos", pos);
                this.startActivityForResult(intent, RETURN_FROM_SEND_ACTIVITY);
                break;


        }

    }

    /**
     * 初始化我的圈子
     */
    void initCircleActive(){

        if(isFirstCircle){
            isFirstCircle = false;
            circleActiveListView = (ExListView)circleView.findViewById(R.id.view_listView);
            circlePreProgress = (ProgressBar)circleView.findViewById(R.id.progressBar);
            cireclePreText = (TextView)circleView.findViewById(R.id.text);



            circlePreProgress.setVisibility(View.VISIBLE);
            cireclePreText.setVisibility(View.VISIBLE);
            circleList = new ArrayList<Active>();
            circleAdapter = new ActiveListViewAdapter(getApplicationContext(),circleList,circleActiveListView);
            circleActiveListView.setAdapter(circleAdapter);
            circleActiveListView.setOnScrollListener(circleAdapter);


            circleActiveListView.setOnUdateNewListender(new ExListView.UdateNewListener(){
                @Override
                public void updateNew() {

                    hideCirclePreView();
                    isUpdateTop = true;
                    Message msg = new Message();
                    msg.what = ActiveActivity.HANDLER_NEW_UPDATE;
                    msg.arg2 = 0;
                    handler.sendMessage(msg);
                }
            });

            circleActiveListView.setOnUdateOldListener(new ExListView.UdateOldListener() {
                @Override
                public void updateOld() {
                    isUpdateTop = false;
                    Message msg = new Message();
                    msg.what = ActiveActivity.HANDLER_OLD_UPDATE;
                    msg.arg2 = 0;
                    handler.sendMessage(msg);

                }
            });

            //先读取本地缓存
            try {
                tempNewCircleList = FileUtil.readJsonToFile(FileUtil.activeCircleCachePath+User.getInstance().getId()+User.getInstance().getPhone()
                                                                                                         +FileUtil.activeCircleFileEndName,0,11);
                if(tempNewCircleList != null &&tempNewCircleList.size()  <=  0){//本地没有缓存动态，到服务器里取动态
                    getCircieActive();
                }else{
                    //有数据
                    Message msg = new Message();
                    msg.what = HttpUtil.GET_ACTIVE_SUCCESS;
                    //判断返回数据时最新数据还是阅读以前的数据
                    msg.arg1 = HttpUtil.GET_NEW;
                    msg.arg2 = 0;
                    handler.sendMessage(msg);

                }


            } catch (Exception e) {
                getCircieActive();
            }


        }

        if(circleList != null &&  circleList.size() == 0 && !isGetActicve){
            getWorldActive();
            circlePreProgress.setVisibility(View.VISIBLE);
            cireclePreText.setText("正在加载");
            cireclePreText.setVisibility(View.VISIBLE);

        }




    }

    /**
     * 初始化全世界
     */

    void initWorldActive() {

        if(isFirstWorld){
            isFirstWorld = false;
            worldActiveListView = (ExListView)worldView.findViewById(R.id.view_listView);
            worldPreText = (TextView)worldView.findViewById(R.id.text);
            worldPreProgress = (ProgressBar)worldView.findViewById(R.id.progressBar);
            worldActiveListView.setHandler(handler);


            worldList = new ArrayList<Active>();
            worldAdapter = new ActiveListViewAdapter(getApplicationContext(),worldList,worldActiveListView);
            worldActiveListView.setAdapter(worldAdapter);
            worldActiveListView.setOnScrollListener(worldAdapter);

            worldActiveListView.setOnUdateNewListender(new ExListView.UdateNewListener() {
                @Override
                public void updateNew() {

                    hideWorldPreView();
                    isUpdateTop = true;
                    Message msg = new Message();
                    msg.what = ActiveActivity.HANDLER_NEW_UPDATE;
                    msg.arg2 = 1;
                    handler.sendMessage(msg);
                }
            });

            worldActiveListView.setOnUdateOldListener(new ExListView.UdateOldListener() {
                @Override
                public void updateOld() {
                    isUpdateTop = false;
                    Message msg = new Message();
                    msg.what = ActiveActivity.HANDLER_OLD_UPDATE;
                    msg.arg2 = 1;
                    handler.sendMessage(msg);
                }
            });


            //先读取本地缓存
            try {
                tempNewWorldList = FileUtil.readJsonToFile(FileUtil.activeWorldCachePath+User.getInstance().getId()+User.getInstance().getPhone()+FileUtil.activeWorldFileEndName, 0, 11);
                if (tempNewWorldList != null && tempNewWorldList.size() <= 0) {//本地没有缓存动态，到服务器里取动态
                    getWorldActive();
                } else {
                    //有数据
                    Message msg = new Message();
                    msg.what = HttpUtil.GET_ACTIVE_SUCCESS;
                    //判断返回数据时最新数据还是阅读以前的数据
                    msg.arg1 = HttpUtil.GET_NEW;
                    msg.arg2 = 1;
                    handler.sendMessage(msg);

                }
            }catch (Exception e){
                e.printStackTrace();
                getWorldActive();
            }

        }

        if(worldList != null && worldList.size() == 0 && !isGetActicve){
            getWorldActive();
            worldPreProgress.setVisibility(View.VISIBLE);
            worldPreText.setText("正在加载");
            worldPreText.setVisibility(View.VISIBLE);

        }


    }

    class ActiveHandler extends Handler {//handler处理消息


        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }

        @Override
        public void dispatchMessage(Message msg) {

            if(msg.what == HttpUtil.GET_ACTIVE_SUCCESS){//获取动态成功

                if(msg.arg2 == 0) {//我的圈子


                    Log.e("============>更新 ", msg.arg2 +"  位置 ");


                    if(msg.arg1 == HttpUtil.GET_NEW)
                    {
                        if(tempNewCircleList != null && tempNewCircleList.size() != 0) {
                            circleAdapter.notiNewDataChange(tempNewCircleList);
                            circleActiveListView.setSelection(0);
                            tempNewCircleList.clear();
                        }
                    }
                    else
                    {
                        circleAdapter.notiOldDataChange(tempOLdCircleList);
                        tempOLdCircleList.clear();
                    }
                        circleActiveListView.hiddenFooter();
                        circleActiveListView.changeState();

                }

                if(msg.arg2 == 1){//全世界

                    hideWorldPreView();
                    if(msg.arg1 == HttpUtil.GET_NEW)
                    {
                        if(tempNewWorldList != null && tempNewWorldList.size() != 0) {
                            worldActiveListView.setSelection(0);
                            worldAdapter.notiNewDataChange(tempNewWorldList);
                            tempNewWorldList.clear();
                        }
                    }
                    else
                    {
                        worldAdapter.notiOldDataChange(tempOLdWorldList);
                        tempOLdWorldList.clear();
                    }
                    worldActiveListView.hiddenFooter();
                    worldActiveListView.changeState();

                }

                if(msg.arg2 == 3){

//                    ToastCustom.showToast(getApplicationContext(),"更新了两个地方!");


                    if(tempNewCircleList != null && tempNewCircleList.size() != 0){
                        circleAdapter.notiNewDataChange(tempNewCircleList);
                        circleActiveListView.setSelection(0);
                        tempNewCircleList.clear();
                    }


                    if(tempNewWorldList != null && tempNewWorldList.size() != 0){
                        worldAdapter.notiNewDataChange(tempNewWorldList);
                        worldActiveListView.setSelection(0);
                        tempNewWorldList.clear();
                    }



                }

                isGetActicve = false;

//                hideCirclePreView();
//                hideWorldPreView();
                changeState();//隐藏加载的内容

            }
            if(msg.what == HttpUtil.GET_ACTIVE_FAILD) //获取动态失败
            {

                //凡是完成任务就隐藏，不区分是哪一个list 因为当前只是发送一个请求
                    circleActiveListView.hiddenFooter();
                    circleActiveListView.changeState();
                    worldActiveListView.hiddenFooter();
                    worldActiveListView.changeState();

                if(msg.obj != null){


                if(msg.obj.toString().equals("无法连接服务器")){
                    ToastCustom.showToast(getApplicationContext(),msg.obj.toString()+",请稍后再试!");
                    changeState();
                }

                if(msg.obj.toString().equals("101")) {//若是没有登录，就帮助用户登录

                    HttpUtil.reLogin(getApplication(),handler);


                    }else{//其他错误，因为是内部错误，不作提示

//                     ToastCustom.showToast(getApplicationContext(), "获取失败");
                        circleActiveListView.hiddenFooter();
                        circleActiveListView.changeState();
                        worldActiveListView.hiddenFooter();
                        worldActiveListView.changeState();
                    //只有在没登陆情况下不显示没有动态了，否则显示
                        changeState();
                    }

                }

                isGetActicve = false;


            }

            if(msg.what == HttpUtil.GET_ACTIVE_EMPTY){//获取成功，但是内容为空，证明当前动态为最新
                if(isUpdateTop) {//只有更新才有提示
//                    ToastCustom.showToast(getApplicationContext(), "没有了╮(╯▽╰)╭");
                    isUpdateTop = false;
                }
                //隐藏listview里底部和头部
                    circleActiveListView.hiddenFooter();
                    circleActiveListView.changeState();
                    worldActiveListView.hiddenFooter();
                    worldActiveListView.changeState();


                isGetActicve = false;
                changeState();
            }

            if(msg.what == HANDLER_NEW_UPDATE){//listview向上拉更新

                map = new HashMap<String, String>();
                if(msg.arg2 == 0)
                    map.put("lasttime",circleAdapter.getNewUpdateTime());
                else
                    map.put("lasttime",worldAdapter.getNewUpdateTime());
                map.put("num","10");
                map.put("pos",""+pos);
                map.put("way","new");
                tempNewCircleList.clear();

                if(!isGetActicve){
                    if(pos == 0)
                    HttpUtil.apiGetActive(map, tempNewCircleList, handler);
                    else
                        HttpUtil.apiGetActive(map, tempNewWorldList, handler);
                    isGetActicve = true;
                }

                hideCirclePreView();
                hideWorldPreView();
//                changeState();


            }

            if(msg.what == HANDLER_OLD_UPDATE){//listview向下滑动，更新

                map = new HashMap<String, String>();
                if(msg.arg2 == 0)
                    map.put("lasttime",circleAdapter.getOldUpdateTime());
                else
                    map.put("lasttime",worldAdapter.getOldUpdateTime());
                map.put("num","10");
                map.put("pos",""+pos);
                map.put("way","old");
                tempOLdCircleList.clear();
                tempOLdWorldList.clear();

                if(!isGetActicve){
                    if(map.get("pos").equals("0"))
                        HttpUtil.apiGetActive(map, tempOLdCircleList, handler);
                    else
                        HttpUtil.apiGetActive(map, tempOLdWorldList, handler);
                    isGetActicve = true;
                }

                hideCirclePreView();
                hideWorldPreView();
//                changeState();

            }


            if(msg.what == HttpUtil.GET_ACTIVE_SINGLE_SUCCESS){//获取自己动态成功 根据内容判断是否

//                Log.e("进入===================>",msg.arg2+""+tempNewCircleList.get(0).toString());

                //若是都不为空就更新 并且根据分享到的位置更新
                 switch(msg.arg2){
                    case 0:
                        if(circleAdapter != null)
                        {
                            circleAdapter.notiNewSingleDataChange(tempNewCircleList);
                            circleActiveListView.setSelection(0);
                        }
                        tempNewCircleList.clear();
                        break;
                    case 1:
                        if(worldAdapter != null)
                        {
                            worldAdapter.notiNewSingleDataChange(tempNewWorldList);
                            worldActiveListView.setSelection(0);
                        }
                        tempNewWorldList.clear();
                        break;
                    case 2:

                        if(circleAdapter != null)
                        {
                            circleAdapter.notiNewSingleDataChange(tempNewCircleList);
                            circleActiveListView.setSelection(0);
                        }
                        if(worldAdapter != null)
                        {
                            worldAdapter.notiNewSingleDataChange(tempNewCircleList);
                            worldActiveListView.setSelection(0);
                        }
                        tempNewCircleList.clear();
                        tempNewWorldList.clear();
                        break;

                }
                isGetActicve = false;
                changeState();


            }

            if(msg.what == HttpUtil.GET_ACTIVE_SINGLE_FAILD){//获取自己动态失败
                if(msg.obj != null){
                    if(msg.obj.toString().equals("101")){
                        HttpUtil.reLogin(getApplication(),handler);
                    }
                }
                changeState();
            }

            if(msg.what == HttpUtil.LOGIN_SUCCESS){

//                changeState();
                //沿用上一个map确保不会错
                tempNewCircleList.clear();
                tempNewWorldList.clear();
                tempOLdCircleList.clear();
                tempOLdWorldList.clear();
                //若是在加载一次就加载了两次
                if(pos == 0)
                {
                    map.put("pos",pos+"");
                    HttpUtil.apiGetActive(map,tempNewCircleList,handler);

                }
                else
                {
                    map.put("pos",pos+"");
                    HttpUtil.apiGetActive(map,tempNewWorldList,handler);
                }
                isGetActicve = true;
//                changeState();

            }

            if(msg.what == HttpUtil.LOGIN_FAILD){
                ToastCustom.showToast(getApplicationContext(),"无法连接服务器，请稍后再试");
                changeState();
                isGetActicve = false;
            }


        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        Log.d("================================>","又返回");

       switch (requestCode){

           case RETURN_FROM_SEND_ACTIVITY://发动动态返回，需要更新界面

               if(resultCode == RESULT_OK){//返回成功


                   //获取自己的最新发送的动态
                   if(data.getIntExtra("size",1) > 1){//两个地方都更新了

//                       map = new HashMap<String, String>();
//                       map.put("pos", "2");
//                       map.put("size","2");
//                       tempNewCircleList.clear();
//                       HttpUtil.apiGetSingleActive( map,tempNewCircleList,handler);
                       map = new HashMap<String, String>();
                       map.put("lasttime", circleAdapter.getNewUpdateTime());
                       map.put("num", "10");
                       map.put("pos", "0");
                       map.put("way", "new");
                       map.put("size","2");
                       tempNewCircleList.clear();
                           HttpUtil.apiGetActive(map, tempNewCircleList, handler);
                           isGetActicve = true;


                       map.clear();
                       map.put("lasttime", worldAdapter.getNewUpdateTime());
                       map.put("num", "10");
                       map.put("pos", "1");
                       map.put("way", "new");
                       map.put("size","2");
                       tempNewWorldList.clear();
                       HttpUtil.apiGetActive(map, tempNewWorldList, handler);
                       isGetActicve = true;



                   }else {
                       if(data.getIntExtra("pos",0) == 0 ){
                           //选择单项就发送
                           map = new HashMap<String, String>();
                           map.put("lasttime", circleAdapter.getNewUpdateTime());
                           map.put("num", "10");
                           map.put("pos", "0");
                           map.put("way", "new");
                           tempNewCircleList.clear();
                           if (!isGetActicve) {
                               HttpUtil.apiGetActive(map, tempNewCircleList, handler);
                               isGetActicve = true;
                           }
                       }else{
                           //选择单项就发送
                           map = new HashMap<String, String>();
                           map.put("lasttime", worldAdapter.getNewUpdateTime());
                           map.put("num", "10");
                           map.put("pos", "1");
                           map.put("way", "new");
                           tempNewWorldList.clear();
                           if (!isGetActicve) {
                               HttpUtil.apiGetActive(map, tempNewWorldList, handler);
                               isGetActicve = true;
                           }
                       }

                   }

               }

               break;

       }

    }

    /**
     * 获得最新的圈子消息
     */
    void getCircieActive(){
        map = new HashMap<String, String>();
        map.put("lasttime","111111111111");
        map.put("num","10");
        map.put("pos","0");
        map.put("way","new");
        tempNewCircleList.clear();
        if(!isGetActicve){
            HttpUtil.apiGetActive(map, tempNewCircleList, handler);
            isGetActicve = true;
        }
    }

    /**
     * 获得最新的世界消息
     */
    void getWorldActive(){
        map = new HashMap<String, String>();
        map.put("lasttime", "111111111111");
        map.put("num", "10");
        map.put("pos", "1");
        map.put("way", "new");
        tempNewWorldList.clear();
        if (!isGetActicve) {
            HttpUtil.apiGetActive(map, tempNewWorldList, handler);
            isGetActicve = true;
        }

    }

    void changeState(){
        if(circleList != null && circleList.size() != 0)
            hideCirclePreView();
        else
            withoutThingCircle();
        if(worldList != null && worldList.size() != 0)
            hideWorldPreView();
        else
            withoutThingWorld();
    }

    void hideCirclePreView(){
        if(cireclePreText != null) {
            cireclePreText.setVisibility(View.GONE);
            circlePreProgress.setVisibility(View.GONE);
        }
    }

    void withoutThingCircle(){
        if(cireclePreText != null) {
            cireclePreText.setVisibility(View.VISIBLE);
            cireclePreText.setText("没有动态");
            circlePreProgress.setVisibility(View.GONE);
        }
    }

    void hideWorldPreView(){
        if(worldPreText != null) {
            worldPreText.setVisibility(View.GONE);
            worldPreProgress.setVisibility(View.GONE);
        }
    }

    void withoutThingWorld(){
        if(worldPreText != null) {
            worldPreText.setVisibility(View.VISIBLE);
            worldPreText.setText("没有动态");
            worldPreProgress.setVisibility(View.GONE);
        }
    }

    /**
     * 保存缓存
     */
    @Override
    protected void onStop() {

        if(circleAdapter != null)
            circleAdapter.saveCircleToFile();
        if(worldAdapter != null)
            worldAdapter.saveWorldToFile();

        super.onStop();
    }

    @Override
    protected void onDestroy() {

        clear();

        super.onDestroy();
    }

    @Override
    protected void onRestart() {

        if(tempNewCircleList == null)
        tempNewCircleList = new ArrayList<Active>();
        if(tempOLdCircleList == null)
            tempOLdCircleList = new ArrayList<Active>();
        if(tempOLdWorldList == null)
            tempOLdWorldList = new ArrayList<Active>();
        if(tempNewWorldList == null)
            tempNewWorldList = new ArrayList<Active>();
        if(circleList == null)
            circleList = new ArrayList<Active>();
        if(worldList == null)
            worldList = new ArrayList<Active>();


        super.onRestart();
    }

    void clear(){
        circleList = null;
        worldList = null;
        tempNewCircleList = null;
        tempOLdCircleList = null;
        tempNewWorldList = null;
        tempOLdWorldList = null;
    }

}
